package io.kiki.sba.registry.common.model.wrapper;

import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;


public class WrapperInvocation<T, R> {

    private final Wrapper<T, R> target;

    private Iterator<WrapperInterceptor> iterator;

    public WrapperInvocation(Wrapper<T, R> target, List<WrapperInterceptor> wrapperInterceptorList) {
        this.iterator = wrapperInterceptorList.iterator();
        this.target = target;
    }

    public R proceed() throws Exception {
        if (iterator.hasNext()) {
            WrapperInterceptor<T, R> wrapperInterceptor = iterator.next();
            return wrapperInterceptor.invokeCodeWrapper(this);
        }
        return target.call();
    }


    public Supplier<T> getParameterSupplier() {
        return target.getParameterSupplier();
    }
}
